clear all
set more off
cd "/Users/yasenov/Dropbox (IPL)/RefugeeBan/data/"

******************************
*********** PART 1 *********** 
******************************

use "WRAPS/wraps_fips.dta"
keep if inlist(year, 2010, 2016, 2017)

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
drop if _merge == 1    
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3) keepusing(pop_total)
drop if _merge == 1   
drop _merge

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

gen lrefugees = log(refugees + 1)
gen rrefugees = (refugee / pop_total) * 100

sum

****************
****************

reshape wide lproperty rproperty rviolent lviolent rrefugees lrefugees pop_total refugees property violent, i(fips) j(year)

gen d_10_16_rproperty_crime = rproperty_crime2016 - rproperty_crime2010
gen d_10_16_rviolent_crime = rviolent_crime2016 - rviolent_crime2010

gen d_10_16_lproperty_crime = lproperty_crime2016 - lproperty_crime2010
gen d_10_16_lviolent_crime = lviolent_crime2016 - lviolent_crime2010

gen d_16_17_rrefugees = rrefugees2017 - rrefugees2016
gen d_16_17_lrefugees = lrefugees2017 - lrefugees2016

****************
****************

local if if abs(d_10_16_rproperty_crime) <= 1000 & d_16_17_rrefugees > -.4

twoway (scatter d_10_16_rproperty_crime d_16_17_rrefugees `if' [w = pop_total2016], msymbol(circle_hollow)) ///
	(lowess d_10_16_rproperty_crime d_16_17_rrefugees `if' ), ///
	ytitle("{&Delta} Property Crimes Rate {superscript: 2010-2016}", size(med)) ///
	xtitle("{&Delta} Refugee Arrivals per Capita {superscript: pre-post}", size(med)) ///
	legend(off) ///
	title("Property Crimes") ///
	name(graph1_a, replace)

local if if abs(d_10_16_rviolent_crime) <= 1000 & d_16_17_rrefugees > -.4	
	
twoway (scatter d_10_16_rviolent_crime d_16_17_rrefugees `if' [w = pop_total2016], msymbol(circle_hollow)) ///
	(lowess d_10_16_rviolent_crime d_16_17_rrefugees `if' ), ///
	ytitle("{&Delta} Violent Crimes Rate {superscript: 2010-2016}", size(med)) ///
	xtitle("{&Delta} Refugee Arrivals per Capita {superscript: pre-post}", size(med)) ///
	legend(off) ///
	title("Violent Crimes") ///
	name(graph1_b, replace) ///
	ylabel(-500(250)500)

****************
****************

local if if abs(d_10_16_lproperty_crime) <= 1 // & d_16_17_lrefugees > -.4

twoway (scatter d_10_16_lproperty_crime d_16_17_lrefugees `if' [w = pop_total2016], msymbol(circle_hollow)) ///
	(lowess d_10_16_lproperty_crime d_16_17_lrefugees `if' ), ///
	ytitle("% {&Delta} Property Crimes {superscript: 2010-2016}", size(med)) ///
	xtitle("% {&Delta} Refugee Arrivals {superscript: pre-post}", size(med)) ///
	legend(off) ///
	title("Property Crimes") ///
	name(graph1_c, replace)

local if if abs(d_10_16_lviolent_crime) <= 1 // & d_16_17_lrefugees > -.4	
	
twoway (scatter d_10_16_lviolent_crime d_16_17_lrefugees `if' [w = pop_total2016], msymbol(circle_hollow)) ///
	(lowess d_10_16_lviolent_crime d_16_17_lrefugees `if' ), ///
	ytitle("% {&Delta} Violent Crime {superscript: 2010-2016}", size(med)) ///
	xtitle("% {&Delta} Refugee Arrivals {superscript: pre-post}", size(med)) ///
	legend(off) ///
	title("Violent Crimes") ///
	name(graph1_d, replace)


graph combine graph1_a graph1_b graph1_c graph1_d , imargin(small)
graph export "../results/fig_d2010_16_pretrends.pdf", replace

******************************
*********** PART 2 *********** 
******************************

use "WRAPS/wraps_fips.dta", clear
keep if year == 2016
merge 1:1 fips year using "demographics/demographics.dta", nogen keep(3)

gen rrefugees = (refugees / pop_total ) * 100000
xtile group = refugees, n(3)
tab group, m

keep fips group
tempfile edno
save `edno'

****************
****************

use "WRAPS/wraps_fips.dta", clear
keep if year >= 2010

merge 1:1 fips year using "crime/crime.dta", keep(1 3) keepusing(property violent)
drop if _merge == 1    
drop _merge

merge 1:1 fips year using "demographics/demographics.dta", keep(1 3)
drop if _merge == 1   
drop _merge

merge m:1 fips using `edno', nogen
tab group, m

gen lproperty_crime = log(property_crime + 1)
gen rproperty_crime = (property_crime / pop_total) * 100000

gen lviolent_crime = log(violent_crime + 1)
gen rviolent_crime = (violent_crime / pop_total) * 100000

gen lrefugees = log(refugees + 1)
gen rrefugees = (refugee / pop_total) * 100

sum

****************
****************

collapse (mean) lproperty_crime rproperty_crime lviolent_crime rviolent_crime, by(group year) //  [aw = pop_total]
sum

twoway (line rproperty_crime year if group == 1, lpattern(solid)) ///
	(line rproperty_crime year if group == 2, lpattern(longdash)) ///
	(line rproperty_crime year if group == 3, lpattern(shortdash) xline(2016.5)), ///
	legend(off) xtitle("Year", size(med)) ///
	ytitle("Property Crimes Rate", size(med)) ///
	title("Property Crimes") ///	
	name(one, replace) 


twoway (line rviolent_crime year if group == 1, lpattern(solid)) ///
	(line rviolent_crime year if group == 2, lpattern(longdash)) ///
	(line rviolent_crime year if group == 3, lpattern(shortdash) xline(2016.5)), ///
	legend(off) xtitle("Year", size(med)) ///
	ytitle("Violent Crimes Rate", size(med)) ///
	title("Violent Crimes") ///	
	name(two, replace)

****************
****************

twoway (line lproperty_crime year if group == 1, lpattern(solid)) ///
	(line lproperty_crime year if group == 2, lpattern(longdash)) ///
	(line lproperty_crime year if group == 3, lpattern(shortdash) xline(2016.5)), ///
	legend(off) xtitle("Year", size(med)) ///
	ytitle("Log Property Crimes", size(med)) ///
	title("Property Crimes") ///	
	name(three, replace)

twoway (line lviolent_crime year if group == 1, lpattern(solid)) ///
	(line lviolent_crime year if group == 2, lpattern(longdash)) ///
	(line lviolent_crime year if group == 3, lpattern(shortdash) xline(2016.5)), ///
	legend(off) xtitle("Year", size(med)) ///
	ytitle("Log Violent Crimes", size(med)) ///
	title("Violent Crimes") ///	
	name(four, replace) 


graph combine one two three four , imargin(small)
graph export "../results/fig_d2010_16_pretrends2.pdf", replace
